(function($) {
	
	$.fn.avatarPopup = function(){
		
		return this.each(function(){
			
			var timeDelay = null,
				//contextPath = $("#context-path").val(),
				contextPath = $.global.getContextPath(),
			    avatarImg = $("img.to_popup"),
				followForm = $('.popup_layer .pop_buttons form'), 
				anonymousLoginButton =$('#anonymous-login');
				
			var userPopLayerDOM = $('<div id="user_popup_layer"></div>');
			var prePopLayerDOM =$('<div class="popup_layer" id="pre_popup_layer" style="display:none;">' 
				            + '<table>' 
				            + '<tr>' 
				            + 		'<td class="pop_top_l"></td>' 
				            + 		'<td class="pop_top_c" width="255px"></td>' 
				            + 		'<td class="pop_top_r"></td>'
				            + '</tr>'
				            + '<tr>'
				            + 		'<td class="pop_mid_l"></td>'
				            + 		'<td class="pop_mid_c">'
				            +    	'<div>'
				            +    	'	<dl class="pop_body">'
				            +   			'<dt></dt>'
				            +    			'<dd>正在努力加载中...请耐心等待...</dd>'
				            +    		'</dl>'
				            +    		'<div class="pop-layer-arrow pop-arrow-position"></div>'
				            +       '</div>'
				            +   	'</td>'
				            +   	'<td class="pop_mid_r"></td>'
				            + '</tr>'
				            +'<tr>'
				           	+	'<td class="bottom_l"></td>'
				            +   '<td class="bottom_c"></td>'
				            +   '<td class="bottom_r"></td>'
				            +'</tr>'
				            +'</table>'
				            +'</div>');
				               
			var prePopLayer = $(prePopLayerDOM).appendTo('body');
			var userPopLayer = $(userPopLayerDOM).appendTo('body');
			
			$(avatarImg).live("mouseenter",function(event){
				var position = $(this).offset(),
					userId = $(this).next().val(),
					profileUrl = contextPath + '/profile/popup?id=' + userId;
				
				timeDelay = setTimeout(function(){
						$(prePopLayer).show().css('position','absolute')
													          .css('top',position.top - 116)
												 	          .css('left',position.left - 40);
						$.ajax({
							url: profileUrl,
							type:'GET',
							dataType:'html',
							beforeSend: function (xhr) {
								 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
							},
							success:function(html){
								$(userPopLayer).html(html);
								$(prePopLayer).hide();
								$(userPopLayer).css('z-index','1000')
											   .css('position','absolute')
							     			   .css('top',position.top - 116)
							     			   .css('left',position.left - 40).show();
							}
						});
				},700);
				
				
			}).live("mouseleave", function(){
				$(userPopLayer).delay(50).fadeOut(400);
				clearTimeout(timeDelay);
			});
			
			$("#user_popup_layer").live('mouseenter',function(){
				$(this).stop(true, true);
			}).live("mouseleave", function(){
				$(this).delay(50).fadeOut(400);
				clearTimeout(timeDelay);
			});
			
			//头像浮层的关注
			$(followForm).live('submit',function(){
				
				var $follow_action_btn = $(this).find('.follow_action_btn');
				$.ajax({
					async:false,
					url:$(this).attr('action'),
					dataType:'json',
					type:'POST',
					data:$(this).serialize(),
					async:false,
					beforeSend: function (xhr) {
						 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
					},
					statusCode: {
						405: function() {
							$(anonymousLoginButton).click();
						},
						404: function() {
					   	 	location.href = contextPath + '/';
						},
						403: function() {
							$(anonymousLoginButton).click();
						}
					},
					success:function(json){
						if(json.success){
							var fansNum = parseInt($('#pop_fans_num').text());
							
							if($follow_action_btn.hasClass('cancel-follow-btn')){
								$('#pop_remove_follow').hide();
								$('#pop_add_follow').fadeIn('slow');
								$('#pop_fans_num').text(fansNum-1);
							}
							if($follow_action_btn.hasClass('follow-btn')){
								$('#pop_add_follow').hide();
								$('#pop_remove_follow').fadeIn('slow');
								$('#pop_fans_num').text(fansNum+1);
							}
						}
					}
				});
				
				return false;
			});
			
		});
	};
})(jQuery);